/**
 *
 */
package com.example.controller;


import com.example.util.DateUtils;
import com.example.util.Md5Util;
import com.example.util.RSAUtil3;
import com.example.util.SignUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author: luochao
 * @since: 2020年2月8日  下午1:12:27
 * @history:
 */
@RestController
@RequestMapping("/pay")
public class IntexController extends BaseController {

    private static CloseableHttpClient httpsclient = HttpClientBuilder.create().build();

    @Value("${pay.url}")
    private String url;
    @Value("${pay.publicKey}")
    private String publicKey;


    /**
     * 下单
     *
     * @param
     * @return
     * @throws Exception
     * @create 2020年2月8日 下午2:41:20 luochao
     * @history
     */
    @RequestMapping("/generateOrder")
    public String generateOrder() throws Exception {
        HttpPost httpPost = new HttpPost(url+"/pay/generateOrder");
        Map<String, String> map = new HashMap<>();
        map.put("tranNo", "210831135252309694262");//订单号
        map.put("money", "1");//消费金额
        map.put("prodName", "洗车测试");//商品名称
        map.put("tranType", "洗车消费");//消费类型

        map.put("notifyUrl", "");//回调地址
        map.put("time", DateUtils.getFormat());//当前时间,精确到秒，格式如：20090202080403
        map.put("account", "9");//校园卡号
        map.put("account_type", "1");//类型 固定校园卡为1
        map.put("returnUrl", "");//支付成功跳转页面
        //学号：YM201, 密码：123456, 名字：云马 201, 一卡通账号：9
        String signParams = Md5Util.md5(SignUtil.getSignParams(map));
        map.put("sign", RSAUtil3.encryptByPublicKey(signParams, publicKey));
        httpPost.setEntity(assembleParam(map, ContentType.APPLICATION_FORM_URLENCODED));
        CloseableHttpResponse response = httpsclient.execute(httpPost);
        String result = toString(response.getEntity());
        return result;
    }











    /**
     * 身份认证
     *
     * @param
     * @return
     * @throws Exception
     * @create 2020年2月8日 下午2:41:20 luochao
     * @history
     */
    @RequestMapping("/bindingCampusCard")
    public String bindingCampusCard() throws Exception {
        HttpPost httpPost = new HttpPost(url+"/pay/bindingCampusCard");
        Map<String, String> map = new HashMap<>();
        //自定义参数，项目中以实际为准
        map.put("realName", "云马201");//真实姓名，
        map.put("incomeAccount", "9");//账号
        map.put("incomeType", "1");//账号类型： 1 一卡通账号 2 学号 4 身份证(兼容原值 3)
        map.put("schoolCode", "yq025");//学校编码
        map.put("time", DateUtils.getFormat());//当前时间,精确到秒，格式如：20090202080403
        //学号：YM201, 密码：123456, 名字：云马 201, 一卡通账号：9
        String signParams = Md5Util.md5(SignUtil.getSignParams(map));
        map.put("sign", RSAUtil3.encryptByPublicKey(signParams, publicKey));
        httpPost.setEntity(assembleParam(map, ContentType.APPLICATION_FORM_URLENCODED));
        CloseableHttpResponse response = httpsclient.execute(httpPost);
        String result = toString(response.getEntity());
        return result;
    }

/**  相应参数
 * { "statusCode": 0, "message": "操作成功", "data": { "realName": "云马 201", "cardMoney": "6805.2", "studentNumber": "YM201", "sex": "女", "userCard": "9" },"success": true }
 */

    /**
     * 退款
     *
     * @param
     * @return
     * @throws Exception
     * @create 2020年2月8日 下午2:41:20 luochao
     * @history
     */
    @RequestMapping("/refund")
    public String refund() throws Exception {
        HttpPost httpPost = new HttpPost(url+"/refund");
        Map<String, String> map = new HashMap<>();
        //自定义参数，项目中以实际为准
        map.put("cpTranNo", "210831135252309694262");//退款订单号
        map.put("refundTranNo", "2109011647525301011");//退款单号
        map.put("refundMoney", "1");//退款金额  单位：分
        map.put("refundReason", "订单取消正常退款");//退款原因
        map.put("notifyUrl", "");//回调地址
        map.put("time",DateUtils.getFormat());//当前时间,精确到秒，格式如：20090202080403

        //学号：YM201, 密码：123456, 名字：云马 201, 一卡通账号：9
        String signParams = Md5Util.md5(SignUtil.getSignParams(map));
        map.put("sign", RSAUtil3.encryptByPublicKey(signParams, publicKey));
        httpPost.setEntity(assembleParam(map, ContentType.APPLICATION_FORM_URLENCODED));
        CloseableHttpResponse response = httpsclient.execute(httpPost);
        //result返回为空或未返回错误信息及代表测试成功
        String result = toString(response.getEntity());
        return result;
    }



    /**
     * 回调
     *
     * @param
     * @return
     * @throws Exception
     * @create 2020年2月8日 下午2:41:20 luochao
     * @history
     */
    @RequestMapping(path = "/receive")
    public String receive(HttpServletRequest req) throws Exception {
        Map<String, String> map = getRequestParmeter(req);
        String signParams = Md5Util.md5(SignUtil.getSignParams(map));
        String sign = req.getParameter("sign");
        String decryptByPrivateKey = RSAUtil3.decryptByPublicKey(sign, publicKey);
        boolean equals = signParams.equals(decryptByPrivateKey);// 验签    true  false

        return "{\"success\":true,\"completeTime\":\"yyyy-MM-dd HH:mm:ss\"}";
    }

}
